<?php foreach ($sequences as $sequence): ?>
	<div class="camera left">
		<?php
		$url = $this->createUrl('show', array('id'=>$sequence));
		$htmlOptions = array('id'=>"camera-{$sequence}");
		echo CHtml::image($url, "Camera-{$sequence}", $htmlOptions);
		?>
		<div class="note center">
			<?php
			$params = array(':sequence'=>$sequence);
			$camera = Camera::model()->find('sequence = :sequence', $params);
			$name = $camera ? $camera->location : 'N/A';
			echo "{$name},";
			?>
			<span id="camera-location-<?php echo $sequence; ?>">
				<?php
				$camera = new Camera;
				$camera->sequence = $sequence;
				$time = 'N/A';
				if ($camera->imageExists) {
					$time = $camera->modifiedTime;
				}
				echo $time;
				?>
			</span>
		</div>
	</div>
<?php endforeach; ?>
<div class="clear"></div>

<?php $url = $this->createAbsoluteUrl('getTime'); ?>
<script type="text/javascript">
	var sequences = [<?php echo implode(', ', $sequences); ?>];
	
	function refresh() {
		var sequence = sequences.shift();
		
		var img = $("#camera-" + sequence);
		var src = img.attr("src");
		var queryPos = src.indexOf('?');
		if (queryPos != -1) {
			src = src.substring(0, queryPos);
		}
		img.attr('src', src + '?' + Math.random());
		
		$.ajax({
			type: "GET",
			dataType: "json",
			url: "<?php echo $url; ?>",
			data: "sequence=" + sequence,
			success: function(data) {
				var span = $("#camera-location-" + data["sequence"]);
				span.text(data["time"]);
			}
		});
		
		sequences.push(sequence);
		setTimeout(refresh, <?php echo $delay; ?>);
	}
</script>

<?php
$script = "setTimeout(refresh, 5000);";
Yii::app()->clientScript->registerScript('init', $script);
?>
